﻿Option Explicit On

'*******************************************************************
' Program: pkgVisio
' Author: Albert E Edlund
' Date:
'
' Purpose:
' Working with office object XML
' ecma standard site
' http://www.ecma-international.org/publications/standards/Ecma-376.htm
'
' three primary files, app.xml, core.xml, document.xml
'
'*******************************************************************

#Region " imports "

Imports System.Text
Imports System.Collections
Imports System.Collections.Generic
Imports System.Collections.ObjectModel

Imports System.Xml
Imports System.Linq
Imports System.Linq.Enumerable
Imports System.Windows.Input

Imports Microsoft.Practices.EnterpriseLibrary.Common
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging

Imports DocumentFormat.OpenXml


' visio v15 does not use a xName prefix which may put it into conflict with other open office apps
Imports <xmlns="http://schemas.microsoft.com/office/visio/2012/main">
' generic namespace for office open xml, used in all of them
Imports <xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">



Imports pkgVisio.clsVisPackage
Imports pkgVisio.clsStructures

#End Region



Public Class frmDocument


#Region " Properties and Declarations "




    '
    ' what is the latest document
    '
    Public _xDoc As XDocument
    Public Property xDoc As XDocument
        Get
            Return Me._xDoc
        End Get
        Set(value As XDocument)
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("Value", _
                    "xDoc is Null.")
            End If
            Me._xDoc = value
        End Set
    End Property


    Public m_visPackage As clsVisPackage
    Public Property visPackage As clsVisPackage
        Get
            visPackage = Me.m_visPackage
        End Get
        Set(value As clsVisPackage)
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("Value", _
                    "visPackage is Null.")
            End If
            Me.m_visPackage = value
        End Set
    End Property

    Private m_visDocument As clsDocument
    Public Property visDocument As clsDocument
        Get
            visDocument = Me.m_visDocument
        End Get
        Set(value As clsDocument)
            Me.m_visDocument = value
        End Set
    End Property


#End Region



    '
    '
    '
    Private Sub AboutToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles AboutToolStripMenuItem.Click
        frmAbout.ShowDialog()
    End Sub

    '
    '
    '
    Private Sub ExitToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub


    '
    ' throw not implemented exception
    '
    Private Sub FutureFeature()
        Throw New NotImplementedException
    End Sub

    '    Try
    '        FutureFeature()
    '    Catch NotImp As NotImplementedException
    '        MsgBox(NotImp.Message)
    '    End Try

    '
    '
    '
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        Me.m_visDocument = Nothing
        Me.visDocument = New clsDocument

        Me.m_visPackage = Nothing
        Me.visPackage = New clsVisPackage



    End Sub


    '
    '
    '
    Private Sub frmDocument_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed


        Me.lvAppXml.Clear()
        Me.lvCoreXml.Clear()
        Me.lvDocXml.Clear()
        Me.lvEvents.Clear()


    End Sub



    '
    '
    '
    Private Sub frmDocument_Shown(sender As Object, e As System.EventArgs) Handles Me.Shown

        Try


            Me.visPackage.nsDict.Clear()
            Me.visPackage.attDict.Clear()
            Me.visPackage.elemDict.Clear()
            'copy the current package to the document class
            Me.visDocument.visPackage = Me.visPackage

            ' in v15 document defintion is spread across multiple files
            Me.visDocument.xDoc = Me.visDocument.getDocumentPkgPart()
            Me.visPackage.readAttAndElem(Me.visDocument.xDoc, "", "VisioDocument")
            Me.visPackage.loadAttElemLV(Me.lvDocXml, Me.visPackage.elemDict)

            Me.visDocument.xDoc = Me.visDocument.getDocPkgPart("app")
            Me.visPackage.readAttAndElem(Me.visDocument.xDoc, "", "Properties")
            Me.visPackage.loadAttElemLV(Me.lvAppXml, Me.visPackage.elemDict)

            Me.visDocument.xDoc = Me.visDocument.getDocPkgPart("core")
            Me.visPackage.readAttAndElem(Me.visDocument.xDoc, "cp", "coreProperties")
            Me.visPackage.loadAttElemLV(Me.lvCoreXml, Me.visPackage.elemDict)


        Catch errXml As XmlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(errXml, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        Catch err As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(err, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try


    End Sub

    '
    '
    '
    Private Sub lvDocXml_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles lvDocXml.SelectedIndexChanged


        Try

            Dim docelemItems As ListView.SelectedListViewItemCollection = lvDocXml.SelectedItems
            Dim docelemItem As ListViewItem
            Dim docelemSubItem As ListViewItem.ListViewSubItem

            If 0 < docelemItems.Count Then
                docelemItem = docelemItems(0)
                docelemSubItem = docelemItem.SubItems(0)

                Select Case LCase(docelemSubItem.Text)
                    Case "documentsheet"
                        Dim newPs As stcPageShape
                        With newPs
                            .psPage = -1
                            .psShape = -1
                        End With

                        Me.visDocument.xDoc = Me.visDocument.getDocumentPkgPart()
                        frmShape.initShapeData("DocumentSheet", Me.visPackage, visDocument.xDoc, newPs)
                        frmShape.ShowDialog(Me)

                    Case "eventlist"
                        Me.visDocument.xDoc = Me.visDocument.getDocumentPkgPart()
                        Me.visPackage.readAttAndElem(Me.visDocument.xDoc, "", "VisioDocument")
                        Me.visPackage.loadAttElemLV(Me.lvDocXml, Me.visPackage.elemDict)
                        Me.visDocument.loadEvents(Me.lvEvents)
                    Case Else
                        Try
                            FutureFeature()
                        Catch NotImp As NotImplementedException
                            MsgBox(NotImp.Message)
                        End Try
                End Select
            End If



        Catch errXml As XmlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(errXml, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        Catch err As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(err, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try
    End Sub



End Class